<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreatemeetingAPIRequest;
use App\Http\Requests\API\UpdatemeetingAPIRequest;
use App\Models\Meeting;
use App\Models\CityModel;
use App\Models\MeetingGuest;
use App\Repositories\MeetingGuestRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class meetingController
 * @package App\Http\Controllers\API
 */

class MeetingGuestAPIController extends AppBaseController
{
    /** @var  MeetingGuestRepository */
    private $MeetingGuestRepository;

    /**
     * Display a listing of the meeting.
     * GET|HEAD /meetings
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->MeetingGuestRepository->pushCriteria(new RequestCriteria($request));
        $this->MeetingGuestRepository->pushCriteria(new LimitOffsetCriteria($request));
        $meetings = $this->MeetingGuestRepository->all();

        return $this->sendResponse($meetings->toArray(), 'Meetings retrieved successfully');
    }

    /**
     * display_order asc
     * @param TicketModelRepository $repo
     */
    public function search(MeetingGuestRepository $repo)
    {
        $searchOptions = $this->normalizeSearchOptions();
        $sortOptions = $this->normalizeSortOptions();
        $sortOptions['sort_key'] = 'display_order';
        $sortOptions['sort'] = 'asc';

        $searchOptions['status'] = 0;
        $searchOptions['meeting_id'] = $this->request->get('meeting_id');

        $ret = $repo->search($searchOptions, $sortOptions);

        $lists = $ret['lists'];
        if (count($lists) == 0) {
            return $this->success(['lists' => []]);
        }

        $ret['lists'] = $lists;

        return $this->success($ret);
    }


    public function apiSearch($id, MeetingGuestRepository $repo)
    {
        $searchOptions = $this->normalizeSearchOptions();
        $sortOptions = $this->normalizeSortOptions();
        $sortOptions['sort_key'] = 'display_order';
        $sortOptions['sort'] = 'asc';

        $searchOptions['status'] = 0;
        $searchOptions['meeting_id'] = $id;

        $ret = $repo->search($searchOptions, $sortOptions);

        $lists = $ret['lists'];
        if (count($lists) == 0) {
            return $this->success(['lists' => []]);
        }

        $ret['lists'] = $lists;

        return $this->success($ret);
    }

    /**
     * @param MeetingGuestRepository $meetingRepo
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(MeetingGuestRepository $meetingRepo)
    {
        $input = $this->request->all();
        if (empty($input['display_order'])) {
            $displayOrder = MeetingGuest::where('meeting_id', $input['meeting_id'])->where('status', 0)->count();
            $displayOrder += 1;
            $input['display_order'] = $displayOrder;
        }
        $userModels = $meetingRepo->create($input);

        return $this->sendResponse($userModels->toArray(), '新增嘉宾成功');
    }

    /**
     * Display the specified meeting.
     * GET|HEAD /meetings/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id, MeetingGuestRepository $MeetingGuestRepository)
    {
        /** @var meeting $meeting */
        $meeting = $MeetingGuestRepository->findWithoutFail($id);

        if (empty($meeting)) {
            return $this->sendError('Meeting not found');
        }

        return $this->sendResponse($meeting->toArray(), 'Meeting retrieved successfully');
    }

    /**
     * Update the specified meeting in storage.
     * PUT/PATCH /meetings/{id}
     *
     * @param  int $id
     * @param UpdatemeetingAPIRequest $request
     *
     * @return Response
     */
    public function update($id, MeetingGuestRepository $MeetingGuestRepository)
    {
        $input = $this->request->all();
        $this->validateInputParams('update', $input);

        /** @var meeting $meeting */
        $meeting = $MeetingGuestRepository->findWithoutFail($id);

        if (empty($meeting)) {
            return $this->sendError('Meeting not found');
        }

        $meeting = $MeetingGuestRepository->update($input, $id);

        return $this->sendResponse($meeting->toArray(), 'meeting updated successfully');
    }

    /**
     * Remove the specified meeting from storage.
     * DELETE /meetings/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var meeting $meeting */
        $meeting = $this->MeetingGuestRepository->findWithoutFail($id);

        if (empty($meeting)) {
            return $this->sendError('Meeting not found');
        }

        $meeting->delete();

        return $this->sendResponse($id, 'Meeting deleted successfully');
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchDestroy()
    {
        $ids = $this->request->get('ids');
        if (empty($ids)) {
            return $this->sendError('id不为空');
        }

        $ids = explode(',', $ids);
        MeetingGuest::whereIn('id', $ids)->update(['status' => 2]);

        return $this->success([], '操作成功');
    }

    /**
     * 参数验证规则
     * self::VALID_RULES[__FUNCTION__]
     */
    const VALID_RULES = [
        'store' => [
            'name' => 'required|string',
            'avatar' => 'required|string',
            'description' => 'required|string',
            'meeting_id' => 'required|int',
        ],
        'update' => [
            'name' => 'string',
            'avatar' => 'string',
            'description' => 'string',
        ],
    ];
}
